package com.weiqiao.mall.repository.impl;

import com.google.gson.Gson;
import com.mongodb.client.model.Filters;
import com.weiqiao.base.dao.impl.BaseMongoDaoImpl;
import com.weiqiao.mall.domain.WXCompanyConfEntity;
import com.weiqiao.mall.repository.ICompanyConfRepository;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Repository;

/**
 * @author PENGLIWEI
 * @createDate 2019-10-29
 */
@Repository("CompanyConfRepositoryImpl")
public class CompanyConfRepositoryImpl extends BaseMongoDaoImpl implements ICompanyConfRepository {
    private final String collectionName = "wx_company_conf";

    @Override
    public WXCompanyConfEntity getCompanyConfEntity(int company_id) {
        Gson gson = new Gson();
        WXCompanyConfEntity entity = new WXCompanyConfEntity();
        Bson filter = Filters.eq("company_id", company_id);
        String result = this.findOne(collectionName, filter);
        if (result.length() > 0) {
            entity = gson.fromJson(result, WXCompanyConfEntity.class);
        }
        return entity;
    }

    @Override
    public boolean updateCompanyConfEntity(int company_id, Document doc) {
        Bson filter = Filters.eq("company_id", company_id);
        boolean result = this.updateOne(collectionName, filter, doc, true);
        return result;
    }

    @Override
    public boolean updateCompanyConfEntity(int company_id, String json) {
        Bson filter = Filters.eq("company_id", company_id);
        Document doc = Document.parse(json);
        boolean result = this.updateOne(collectionName, filter, doc, true);
        return result;
    }

    @Override
    public boolean updateCompanyConfig(Bson filter, Document doc) {
        boolean result = this.updateOne(collectionName, filter, doc, true);
        return result;
    }
}
